package com.tocici.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.tocici.domain.bo.MusicBo;
import com.tocici.domain.vo.ArtistVo;
import com.tocici.domain.vo.CategoryVo;
import com.tocici.domain.vo.MusicVo;
import com.tocici.service.IArtistService;
import com.tocici.service.ICategoryService;
import com.tocici.service.IMusicService;
import lombok.RequiredArgsConstructor;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/ai")
@SaIgnore
public class AIChatController {

    @Autowired
    private IMusicService musicService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IArtistService artistService;

//    private final ChatClient client;

//    @Autowired
//    private Cache<Long, List<String>> caffeine;
    @Autowired
    private StringRedisTemplate redisTemplate;

    ChatMemory chatMemory = new InMemoryChatMemory();

//    public AIChatController(ChatClient.Builder builder) {
//        this.client = builder.defaultSystem("你是一名音乐库的助理，能根据我给你的歌单推荐歌曲")
//            .defaultAdvisors(
//                new MessageChatMemoryAdvisor(
//                    chatMemory,
//                    String.valueOf(1),
//                    10
//                )
//            )
//            .build();
//    }

//    @GetMapping(value = "/chat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<String> chat(String message){
//        System.out.println("message = " + message);
////        List<Message> messages = chatMemory.get(String.valueOf(1), 10);
////        messages.forEach((m) -> {
////            MessageType messageType = m.getMessageType();
////            System.out.println("messageType.getValue() = " + messageType.getValue());
////            System.out.println("m.getText() = " + m.getText());
////        });
//
//        return client.prompt().user(message).stream().content();
////  }
    @GetMapping(value = "/chat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat02(@RequestParam("message") String message) {
        String token = "sk-da61f476f6864905b52da06a96f42d80";
        ChatModel chatModel = new DashScopeChatModel(new DashScopeApi(token));
        //
        ChatClient.Builder builder = ChatClient.builder(chatModel);
        ChatClient chatClientByBuilder = builder.defaultSystem("你是一名音乐库的助理，能根据我给你的歌单推荐歌曲").defaultAdvisors(
            new MessageChatMemoryAdvisor(
                    chatMemory,
                    String.valueOf(1),
                    10
                )
        ).build();
        String musicList = redisTemplate.opsForValue().get("musicList");
        if (musicList == null){
            List<MusicVo> musicVos = musicService.queryList(new MusicBo());
            musicVos.forEach(musicVo -> {
                CategoryVo categoryVo = categoryService.queryById(musicVo.getCategoryId());
                ArtistVo artistVo = artistService.queryById(musicVo.getArtistId());
                musicVo.setArtistVo(artistVo);
                musicVo.setCategoryVo(categoryVo);
            });
            redisTemplate.opsForValue().set("musicList",musicVos.toString());
            musicList = musicVos.toString();
        }

        // 使用ChatClient.create()创建
//         ChatClient chatClientByCreate = ChatClient.create(chatModel);

        return chatClientByBuilder.prompt(musicList)
            .user(message)
            .stream()
            .content();
    }

}
